#include "view_blood_pressure.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_common.h"
#include "Motor.h"
#include "view.h"
#include "times/view_times.h"
#include "times/view_time_0.h"
#include "sports_mode.h"
#include "app_trace.h"
#include "view_blood_pressure.inc"
#include "Heart_rate_additional.h"
#include "blood_oxygen/view_blood_oxygen.h"
#define LOG(fmt, ...) // app_trace_log(fmt "\r\n", ##__VA_ARGS__)
void view_blood_pressure_animation(view_blood_pressure_param const *param)
{
    if (param == 0 || param->file_index_0 < DISP_LFS_FILE_INDEX_BEGIN || param->file_index_0 >= DISP_LFS_FILE_INDEX_END || param->file_index_1 < DISP_LFS_FILE_INDEX_BEGIN ||
        param->file_index_1 >= DISP_LFS_FILE_INDEX_END)
    {
        return;
    }

    if (!param->is_wear)
    {
        _disp_show_flash_img(param->file_index_0, 66, 64, FILE_IMG_T112_not_wear_66_64);
        disp_show_localize((disp_little_fs_file_index_t)param->file_index_1, localize_string_key_AlertNotWear, param->temp_index);
        return;
    }

    int x = 73;
    int y = 28;
    img_gif_com_t const img_cfg = {
        .gif_count = 4,
        .img[0].x = x,
        .img[0].y = y,
        .img[0].sn = FILE_IMG_Blood_Pressure_73_28,
        .img[1].x = x,
        .img[1].y = y,
        .img[1].sn = FILE_IMG_Blood_Pressure_73_28,
        .img[2].x = x,
        .img[2].y = y,
        .img[2].sn = FILE_IMG_Blood_Pressure_2,
        .img[3].x = x,
        .img[3].y = y,
        .img[3].sn = FILE_IMG_Blood_Pressure_2,
    };
    _disp_show_flash_img_gif_all(&img_cfg, param->file_index_0);

    int const font_width = 16;
    int const num_interval = 4;
    int const num_width = font_width + num_interval;
    int const num_height = 37;

    uint8_t const *const second_img = img_common_second;
    int const second_width = 13;
    int const second_height = 27;
    int const second_interval = 4;

    uint8_t const *const mngh_img = bmp_mnhg;
    int const mngh_width = 53;
    int const mngh_height = 17;
    int const mngh_interval = 5;

    uint8_t const *const slash_img = bmp_slash;
    int const slash_width = 13;
    int const slash_height = 37;
    int const slash_interval = 5;

    y = 146;
    if (param->seconds >= 0)
    {
        uint8_t digits = get_num_digit(param->seconds);
        uint32_t width_total = digits * num_width - num_interval + second_width + second_interval;
        int x = (240 - width_total) / 2;
        x = disp_draw_user_image_num(x, y, param->seconds, num_width, num_15x37_TAB) - num_interval;
        x += second_interval;
        disp_draw_user_image(x, y + num_height - second_height, second_img);
    }
    else
    {
        uint8_t num_0_digits = 2;
        uint8_t num_1_digits = 2;
        if (param->dbp != 0 && param->sbp != 0)
        {
            num_0_digits = get_num_digit(param->dbp);
            num_1_digits = get_num_digit(param->sbp);
        }
        uint32_t width_total = num_0_digits * num_width - num_interval + num_1_digits * num_width - num_interval + mngh_width + mngh_interval + slash_width + slash_interval * 2;
        int x = (240 - width_total) / 2;
        if (param->dbp == 0)
        {
            if (disp_ctrl.auto_refresh_count % 2 == 0)
            {
                int x1 = x;
                int y1 = y + num_height - num_height / 2;
                int x2 = x1 + font_width;
                int y2 = y1 + 1;
                disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);

                x1 += num_width;
                x2 = x1 + font_width;
                disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);
            }
            x += num_width * 2 - num_interval;
        }
        else
        {
            x = disp_draw_user_image_num(x, y, param->dbp, num_width, num_15x37_TAB) - num_interval;
        }
        x += slash_interval;
        disp_draw_user_image(x, y + num_height - slash_height, slash_img);
        x += slash_width + slash_interval;
        if (param->sbp == 0)
        {
            if (disp_ctrl.auto_refresh_count % 2 == 0)
            {
                int x1 = x;
                int y1 = y + num_height - num_height / 2;
                int x2 = x1 + font_width;
                int y2 = y1 + 1;
                disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);

                x1 += num_width;
                x2 = x1 + font_width;
                disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);
            }
            x += num_width * 2 - num_interval;
        }
        else
        {
            x = disp_draw_user_image_num(x, y, param->sbp, num_width, num_15x37_TAB) - num_interval;
        }
        x += mngh_interval;
        disp_draw_user_image(x, y + num_height - mngh_height, mngh_img);
    }
}
void view_blood_pressure_display(int view)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        if (disp_ctrl.auto_refresh_count % 10 == 0)
        {
            heart_rate_additional_generate();
        }
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.com_data.i_temp[0] = hr_get_hr_data();
        disp_ctrl.com_data.i_temp[1] = hr_if_band_wear();
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
    }
    // if (disp_ctrl.com_data.i_temp[5])
    // {
    //     disp_draw_user_image(42, 3, img_ble_connect);
    // }
    // else
    // {
    //     disp_draw_user_image(42, 0, img_ble_disconnect);
    // }
    // show_common_time(disp_ctrl.com_data.i_temp[3], disp_ctrl.com_data.i_temp[4]);
    // show_time_0_battery(disp_ctrl.com_data.i_temp[9], disp_ctrl.com_data.i_temp[8]);
    int dbp = 0;
    int sbp = 0;
    if (disp_ctrl.com_data.i_temp[0] != 0)
    {
        dbp = heart_rate_additional_get_diastolic_blood_pressure();
        sbp = heart_rate_additional_get_systolic_blood_pressure();
    }

    view_blood_pressure_param const param = {
        .is_wear = disp_ctrl.com_data.i_temp[1],
        .seconds = -1,
        .dbp = dbp,
        .sbp = sbp,
        .file_index_0 = DISP_LFS_FILE_INDEX_0,
        .file_index_1 = DISP_LFS_FILE_INDEX_1,
        .temp_index = 50,
    };
    view_blood_pressure_animation(&param);
    // int const unit_width = 39;
}
view_event_result_t view_blood_pressure_event(int view, event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        return view_event_result_handled;
    case event_view_entry:
        disp_ctrl.auto_close_delay = 60000;
        hr_test_start(HR_CTRL_TYPE_HR_TEST);
        return view_event_result_handled;
    case event_view_re_screen_on:
        disp_ctrl.auto_close_delay = 60000;
        return view_event_result_handled;
    case event_view_exit:
        hr_test_stop(HR_CTRL_TYPE_HR_TEST);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
